/**
 * This class is the main view for the application. It is specified in app.js as the
 * "autoCreateViewport" property. That setting automatically applies the "viewport"
 * plugin to promote that instance of this class to the body element.
 *
 * TODO - Replace this content of this view to suite the needs of your application.
 */
Ext.define('Admin.view.main.MainController', {
    extend: 'Ext.app.ViewController',

    requires: [
        'Ext.window.MessageBox'
    ],

    alias: 'controller.main',

    config: {
        refs: {
            viewport: 'viewport'
        },
        control: {
            viewport: {
                resize: 'onMainResize'
            }
        }
    },

    onMainResize: function () {
        if (this.showButton && !this.showButton.hidden) {
            this.showButton.setX(document.body.clientWidth - 32);
        }
    },

    hiddenHeaderFooter: function (btn, e) {
        this.getView().down('appheader').hide();
        this.getView().down('appfooter').hide();
        if (!this.showButton) {
            this.showButton = Ext.widget('component', {
                glyph: 0xf013,
                view: this.getView(),
                floating: true,
                x: document.body.clientWidth - 32,
                y: 0,
                height: 4,
                width: 26,
                style: 'background-color: #cde6c7',
                listeners: {
                    el: {
                        click: function (el) {
                            e = Ext.getCmp(el.target.id);
                            e.view.down('appheader').show();
                            e.view.down('appfooter').show();
                            e.hide();
                        }
                    }
                }
            });
        }
        this.showButton.show();
    },

    redirectToHome: function () {
        this.redirectTo('all');
    },

    doLogout: function () {

        // Remove the localStorage key/value
        localStorage.removeItem('AdminLoggedIn');

        // Remove Main View
        this.getView().destroy();

        // Add the Login Window
        Ext.widget('login');
    },

    onExpandAllClick: function (event, target, owner, tool) {
        owner.up('appnavigation').expandAll();
    },

    onCollapseAllClick: function (event, target, owner, tool) {
        owner.up('appnavigation').collapseAll();
    },

    fileUpload: function (btn, e) {
        var form = btn.up('form').getForm();
        if (form.isValid()) {
            form.submit({
                url: '/api/series_manuals/import',
                waitMsg: '正在上传您的文件，请耐心等候...',
                success: function () {
                    Ext.Msg.alert('确认框', '上传文件成功!', function () {
                        btn.up('window').close();
                    }, this);
                },
                failure: function () {
                    Ext.Msg.alert('确认框', '上传文件失败!');
                }
            });
        }
    },

    onClickButton: function () {
        Ext.Msg.confirm('Confirm', 'Are you sure?', 'onConfirm', this);
    },

    onConfirm: function (choice) {
        if (choice === 'yes') {
            this.getView().getViewModel().set('name', '修改后的标题');
        }
    }
});
